#ifndef RUNNABLE_H
#define RUNNABLE_H

#include <visibility.h>
#include "NoCopy.h"

class __EXPORT Runnable : NoCopy {
public:
	Runnable() : state_(NOTSTARTED) {}
	virtual ~Runnable() {}
	virtual void run() = 0;
	
	void run_() {
		state_ = RUNNING;
		run();
		state_ = TERMINATED;
	}
	
	void setExitFlag() {
		state_ = TERMINATING;
	}
	
	bool isRunning() const {
		return state_ == RUNNING || state_ == TERMINATING;
	}
	
protected:
	bool isExitFlag() const {
		return state_ != RUNNING;
	}
	
private:
	enum {NOTSTARTED = 0, RUNNING, TERMINATING, TERMINATED};
	volatile char state_;
};

#endif
